<?php
namespace app\admin\controller;

use think\Db;
use think\Loader;

class Tags extends Base
{
    // 标签列表
    public function index()
    {
        $list = Db::name('tags')->order('id desc')->paginate(10);
        $this->assign('list',$list);
        return $this->fetch();
    }

    // 添加标签
    public function add()
    {
        if(request()->isPost()){
            $data = [
                'tagname' => input('tagname'),
                'addtime' => time(),
            ];
            // 验证信息
            $validate = Loader::validate('Tags');
            if(!$validate->scene('add')->check($data)){
               $this->error($validate->getError());
               die;
            }
            $result = Db::name('tags')->insert($data);
            if($result !== false){
                return $this->success('添加标签成功！','index');
            }else{
                return $this->error('添加标签失败！');
            }
            return;
        }

        return $this->fetch();
    }

    // 编辑标签
    public function edit()
    {
        $id   = input('id');
        $tags = Db::name('tags')->find($id);

        if(request()->isPost()){
            $data = [
                'id'      => input('id'),
                'tagname' => input('tagname'),
            ];

            // 验证信息
            $validate = Loader::validate('Tags');
            if(!$validate->scene('eidt')->check($data)){
               $this->error($validate->getError());
               die;
            }
            $result = Db::name('tags')->update($data);
            if($result !== false){
                $this->success('修改标签成功！','index');
            }else{
                $this->error('修改标签失败！');
            }
            return;
        }

        $this->assign('tags',$tags);
        return $this->fetch();
    }
    
    // 删除 
    public function del()
    {
        $id     = input('id');
        $result = Db::name('tags')->delete(input('id'));

        if($result !== false){
            $this->success('删除标签成功！','index');
        }else{
            $this->error('删除标签失败！');
        }
        
    }  

}
